import {promptBox,toast} from 'nc-lightapp-front';
import { FIELD, CARD_ADD_DISABLED_BUTTON, CARD } from "../../constant";
/**
* @description: 表头编辑后事件
* @param: moduleId 区域编码
* @param: key 当前字段编码
*/
export function afterHeadEvent(props, moduleId, key, value, oldValue) {
        if (key === FIELD.org) {
                let caltime = props.form.getFormItemsValue(CARD.form_id, 'caltime').value

                if(caltime != null){
                        promptBox({
                                color: 'warning',
                                title: "是否变更组织？",
                                content: '变更组织将会清空计算结果',
                                noFooter: false,
                                noCancelBtn: false,
                                beSureBtnClick: ()=>{
                                    //确认
                                    props.form.setFormItemsValue(CARD.form_id, { ['caltime']: {value : null, display:null} });
                                    props.cardTable.setTableData(CARD.table_code, { rows: [] });
                                    afterHeadEvent(props, moduleId, key, value, oldValue);
                                },
                                cancelBtnClick: ()=>{
                                    //取消
                                    props.form.setFormItemsValue(CARD.form_id, { [key]: oldValue });
                                }
                        })

                        return;
                }


                props.resMetaAfterPkorgEdit(); //选择主组织以后，恢复其他字段的编辑性
                props.button.setButtonDisabled(CARD_ADD_DISABLED_BUTTON, false);//恢复增行编辑性
                props.cardTable.setAllCheckboxAble(CARD.table_code, true);
        }else if (key == 'pk_period') {

                let caltime = props.form.getFormItemsValue(CARD.form_id, 'caltime').value

                if(caltime != null){
                        promptBox({
                                color: 'warning',
                                title: "是否变更会计期间？",
                                content: '变更会计期间将会清空计算结果',
                                noFooter: false,
                                noCancelBtn: false,
                                beSureBtnClick: ()=>{
                                    //确认
                                //     props.form.setFormItemsValue(CARD.form_id, 'caltime', {value : null, display:null});
                                props.form.setFormItemsValue(CARD.form_id, { ['caltime']: {value : null, display:null} });
                                    props.cardTable.setTableData(CARD.table_code, { rows: [] });
                                    afterHeadEvent(props, moduleId, key, value, oldValue);
                                },
                                cancelBtnClick: ()=>{
                                    //取消
                                    props.form.setFormItemsValue(CARD.form_id, key, oldValue);
                                }
                        })

                        return;
                }

                if (value != null && value.value != null) {
                        props.form.setFormItemsValue(CARD.form_id, { ['cperiod']: { value: value.refname } });
                        props.form.setFormItemsValue(CARD.form_id, { ['cyear']: { value: value.values.periodyear.value } });
                        props.form.setFormItemsValue(CARD.form_id, { ['cmonth']: { value: value.values.accperiodmth.value } });
                } else {
                        props.form.setFormItemsValue(CARD.form_id, { ['cperiod']: { value: null } });
                        props.form.setFormItemsValue(CARD.form_id, { ['cyear']: { value: null } });
                        props.form.setFormItemsValue(CARD.form_id, { ['cmonth']: { value: null } });
                }
        }
}
